home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_530 / dme / util / autorefs.c
C/C++ Source or Header  |  1992-05-06  |  5KB  |  216 lines

  1.  
  2. /*
  3.  *  AUTOREFS refsfile docfile docfile docfile
  4.  *
  5.  *  Given one or more autodoc or .H files (e.g. intuition.doc) or include
  6.  *  files (e.g. exec/types.h), this program appends to a dme.refs file
  7.  *  <refsfile> appropriate lines.
  8.  *
  9.  *  AUTOREFS determines the file type from the extension (.h for header
  10.  *  files, otherwise assumed to be a doc file).
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. char *SetSName(char *, char *);
  17.  
  18. main(xac, xav)
  19. int xac;
  20. char *xav[];
  21. {
  22.     short i;
  23.     FILE *fi;
  24.     FILE *fo;
  25.     int ac;
  26.     char **av;
  27.  
  28.     expand_args(xac, xav, &ac, &av);
  29.  
  30.     if (ac == 1) {
  31.     puts("autorefs outfile docfile docfile ...");
  32.     puts("AmigaDOS wildcarding works too, btw");
  33.     exit(1);
  34.     }
  35.     fo = fopen(av[1], "a");
  36.     if (!fo) {
  37.     printf("unable to open %s for append\n", av[1]);
  38.     exit(1);
  39.     }
  40.     for (i = 2; i < ac; ++i) {
  41.     char *file = av[i];
  42.     short len = strlen(file);
  43.     short doth = 0;
  44.  
  45.     if (len >= 2 && (file[len-1] == 'h' || file[len-1] == 'H') && file[len-2] == '.')
  46.         doth = 1;
  47.  
  48.     fi = fopen(file, "r");
  49.     if (fi) {
  50.         if (doth) {
  51.         printf("Scanning .H  file: %s\n", file);
  52.         scanhfile(fi, fo, file);
  53.         } else {
  54.         printf("Scanning DOC file: %s\n", file);
  55.         scandocfile(fi, fo, file);
  56.         }
  57.         fclose(fi);
  58.     } else {
  59.         printf("Unable to read %s\n", file);
  60.     }
  61.     }
  62.     return(0);
  63. }
  64.  
  65. /*
  66.  *  Find the headers for each function entry and generate a DME.REFS
  67.  *  entry for it.  The @@<N> is a short form seek position (this field
  68.  *  normally holds a search string).
  69.  */
  70.  
  71. scandocfile(fi, fo, filename)
  72. FILE *fi;
  73. FILE *fo;
  74. char *filename;
  75. {
  76.     char buf[256];
  77.     long pos = 0;
  78.     short lastLineFF = 0;
  79.  
  80.     while (fgets(buf, 256, fi)) {
  81.     short len = strlen(buf) - 1;
  82.     char *ptr = buf + len;
  83.     char *bas = buf;
  84.     char *header, *tail;
  85.  
  86.     buf[len] = 0;
  87.     while (ptr != buf && ptr[-1] != ' ' && ptr[-1] != 9)
  88.         --ptr;
  89.     while (bas < ptr && *bas == 12)
  90.         ++bas;
  91.     if (ptr != bas && *ptr && strncmp(bas, ptr, strlen(ptr)) == 0) {
  92.         if (buf[0] == 12) {
  93.         ++pos;
  94.         buf[0] = 0;
  95.         }
  96.         header = ptr;
  97.         for (ptr = buf + len; ptr != buf && IsAlphaNum(ptr[-1]); --ptr);
  98.         tail = ptr;
  99.         fprintf(fo, "%-20s (^l) %s @@%ld\n", tail, filename, pos);
  100.     } else if (ptr == bas && *ptr && lastLineFF) {
  101.         if (buf[0] == 12) {
  102.         ++pos;
  103.         buf[0] = 0;
  104.         }
  105.         for (ptr = buf + len; ptr != buf && IsAlphaNum(ptr[-1]); --ptr);
  106.         fprintf(fo, "%-20s (^l) %s @@%ld\n", ptr, filename, pos);
  107.     }
  108.     if (buf[0] == ('l'&0x1F))
  109.         lastLineFF = 1;
  110.     else
  111.         lastLineFF = 0;
  112.     pos = ftell(fi);
  113.     }
  114. }
  115.  
  116. /*
  117.  *  Find each structure definition (stupid search, assume struct on left
  118.  *  hand side) then generate dme.refs entry from the end point of the
  119.  *  previous structure to the beginning of the next structure.    That is,
  120.  *  the reference refers to the structure and all fields before and after
  121.  *  it until the next structure (before and after).
  122.  */
  123.  
  124. scanhfile(fi, fo, filename)
  125. FILE *fi;
  126. FILE *fo;
  127. char *filename;
  128. {
  129.     static char buf[256];
  130.     static char sname[128];
  131.     static char lname[128];
  132.     long lin  = 1;
  133.     long lin1;
  134.     long lin2 = 1;
  135.     long pos  = 0;
  136.     long pos1;
  137.     long pos2 = 0;
  138.     short snameisvalid = 0;
  139.     short newsname = 0;
  140.  
  141.     while (fgets(buf, 256, fi)) {
  142.     char *ptr = buf;
  143.  
  144.     if ((ptr = strstr(buf, "struct")) || (ptr = strstr(buf, "union"))) {
  145.         if (ptr[0] == 's')
  146.         ++ptr;
  147.         ptr += 5;
  148.  
  149.         ptr = SetSName(lname, ptr);
  150.  
  151.         /*
  152.          *    search for '{'
  153.          */
  154.  
  155.         {
  156.         while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n' || *ptr == 12)
  157.             ++ptr;
  158.         if (*ptr == 0) {
  159.             short c = ' ';
  160.             long savpos = ftell(fi);
  161.             while (c == ' ' || c == '\t' || c == '\n' || c == 12)
  162.             c = getc(fi);
  163.             ptr[0] = c;
  164.             ptr[1] = 0;
  165.             fseek(fi, savpos, 0);
  166.         }
  167.         }
  168.  
  169.         if (*ptr == '{' && lname[0]) {
  170.         if (snameisvalid)
  171.             fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  172.         strcpy(sname, lname);
  173.         snameisvalid = 0;
  174.         newsname = 1;
  175.         pos1 = pos2;
  176.         lin1 = lin2;
  177.         }
  178.     }
  179.     pos = ftell(fi);
  180.     ++lin;
  181.  
  182.     if (strstr(buf, "};")) {
  183.         pos2 = pos;
  184.         lin2 = lin;
  185.         snameisvalid = newsname;
  186.     }
  187.     }
  188.     if (snameisvalid)
  189.     fprintf(fo, "%-20s %3ld %s @@%ld\n", sname, lin-lin1, filename, pos1);
  190. }
  191.  
  192. char *
  193. SetSName(buf, ptr)
  194. char *buf, *ptr;
  195. {
  196.     while (*ptr == ' ' || *ptr == 9)
  197.     ++ptr;
  198.     while (*ptr && *ptr != '\n' && *ptr != ' ' && *ptr != 9 && *ptr != 12)
  199.     *buf++ = *ptr++;
  200.     *buf = 0;
  201.     return(ptr);
  202. }
  203.  
  204. IsAlphaNum(c)
  205. char c;
  206. {
  207.     if ((c >= 'a' && c <= 'z') ||
  208.     (c >= 'A' && c <= 'Z') ||
  209.     (c >= '0' && c <= '9') ||
  210.     (c == '_') || (c == '(') || (c == ')')
  211.     )
  212.     return(1);
  213.     return(0);
  214. }
  215.  
  216.